import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { Model, ModelPermission, Pageable } from '../domain/sparrow';

@Injectable({
  providedIn: 'root'
})
export class ModelService {

  host:string = "http://localhost:8091/api";

  constructor(private httpClient: HttpClient) { }

  public list(pageable: Pageable){
    let p:Pageable|any = {page: 0, size: 10}
    p.page=pageable.page -1;
    p.size = pageable.size;
    if(pageable.sort)
      p.sort = pageable.sort;
    return this.httpClient.get<any>(this.host + "/models",{params:p});
  }

  public getSysroleModelPermission(pageable: Pageable){
    let p:Pageable|any = {page: 0, size: 10}
    p.page=pageable.page -1;
    p.size = pageable.size;
    if(pageable.sort)
      p.sort = pageable.sort;
    return this.httpClient.get<any>(this.host + "/models/sysroleModelPermissions",{params:p});
  }

  public saveBatch(models: Model[]){
    return this.httpClient.post(this.host+"/models/batch",models);
  }

  public save(model: Model){
    return this.httpClient.post<Model>(this.host+"/models",model);
  }

  public getModelName(){
    return this.httpClient.get<any>(this.host+"/models/getModelName");
  }

  public getModelsInId(ids: string[]|any){
    return this.httpClient.post<any>(this.host + "/models/getModelsInId",ids);
  }

  public addPermission(permissions: ModelPermission){
    return this.httpClient.post<any>(this.host + "/models/permissions",permissions);
  }

  public delPermission(permissions: ModelPermission | any){
    return this.httpClient.delete<any>(this.host + "/models/permissions",{body:permissions});
  }

  delete(ids: string[]) {
    return this.httpClient.delete<any>(this.host + "/models/batch",{body:ids});
  }

}
